package com.cqjtu.store.product.controller;

import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.R;
import com.cqjtu.store.product.pojo.dto.ProductCategoryDTO;
import com.cqjtu.store.product.pojo.dto.ProductCategoryPageDTO;
import com.cqjtu.store.product.pojo.entity.ProductCategory;
import com.cqjtu.store.product.pojo.vo.ProductCategoryVO;
import com.cqjtu.store.product.service.ProductCategoryService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ProjectName: cqjtu-store
 * @Titile: ProductCategoryController
 * @Author: Lucky
 * @Description: 商品分类控制器
 */
@RestController
@RequestMapping("api/category")
@Slf4j
@Api(tags = "商品类别模块")
@CrossOrigin
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;
    /**
     * 查询分类
     * @return
     */
    @ApiOperation("返回所有商品分类")
    @GetMapping("list")
    public R<List<ProductCategoryVO>> getList(){
        List<ProductCategoryVO> list = productCategoryService.getList();
        return R.ok(list);
    }
    @ApiOperation("添加商品分类")
    @PostMapping("add")
    public R<Void> add(@RequestBody @Validated ProductCategoryDTO productCategoryDTO, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        productCategoryService.addCategory(productCategoryDTO,userLoginVO);
        return R.ok();
    }
    /**
     * 删除商品分类(只能删除子分类)
     * @param categoryId
     * @return
     */
    @ApiOperation("删除商品分类")
    @DeleteMapping("delete")
    public R<Void> delete(@RequestParam Integer categoryId, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        productCategoryService.deleteCategory(categoryId,userLoginVO);
        return R.ok();
    }
    @ApiOperation("分页查询商品分类")
    @PostMapping("page")
    public R<PageInfo<ProductCategory>> getListByPage(@RequestBody @Validated ProductCategoryPageDTO productCategoryPageDTO , HttpServletRequest request){
        System.out.println(productCategoryPageDTO);
        String token = request.getHeader("Authorization");
//        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        PageInfo<ProductCategory> pageVO = productCategoryService.getCategoryByPage(productCategoryPageDTO);
        return R.ok(pageVO);
    }

    @ApiOperation("修改商品分类")
    @PutMapping("update")
    public R<Void> update(@RequestBody @Validated ProductCategoryDTO productCategoryDTO, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        productCategoryService.updateCategory(productCategoryDTO,userLoginVO);
        return R.ok();
    }
    @ApiOperation("根据id查询商品分类信息")
    @GetMapping("get")
    public R<ProductCategoryVO> getById(@RequestParam Integer id){
        ProductCategoryVO productCategoryVO = productCategoryService.getCategoryById(id);
        return R.ok(productCategoryVO);
    }

}
